package com.tzz.config;


import com.tzz.entity.ErrorCode;
import com.tzz.entity.HttpResult;
import com.tzz.entity.ResponseException;
import com.tzz.exception.AddExistException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    HttpResult handleException(Exception e) {
        log.error("{}", e);
        log.info("-------------------Exception-------------------");
        return HttpResult.build(ErrorCode.FAIL,e.getMessage());
    }

    @ExceptionHandler(AddExistException.class)
    HttpResult handleProductExustsException(AddExistException pe){
        log.error("{}",pe);
        log.info("-------------------ProductExistException-------------------");
        return HttpResult.build(ErrorCode.FAIL,pe.getMessage());
    }


    /**
     * response异常处理拦截器
     *
     * @param e
     * @return
     */
    @ExceptionHandler()
    HttpResult handleResponseException(ResponseException e) {
        log.error("{}", e);
        return HttpResult.build(e.getErrorCode(), e.getMsg());
    }
}
